home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / drdobbs / 1991 / 02 / snap3.asc < prev    next >
Text File  |  1990-12-26  |  10KB  |  275 lines

  1. _SCREEN CAPTURING FOR WINDOWS 3.0_
  2. by Jim Conger
  3.  
  4. [LISTING ONE]
  5.  
  6. ALL: snap3.exe
  7.  
  8. snap3.obj : snap3.c
  9.      cl -AS -c -DLINT_ARGS -Gsw -Oat -W2 -Zped snap3.c
  10.  
  11. snap3.res:  snap3.rc snap3.ico
  12.      rc -r snap3.rc
  13.  
  14. snap3.exe : snap3.obj snap3.def snap3.res
  15.      link /NOD snap3, , ,libw slibcew, snap3.def
  16.      rc snap3.res
  17.  
  18.  
  19.  
  20. [LISTING TWO]
  21.  
  22. NAME           SNAP3
  23. DESCRIPTION    'snap3 program for windows bitmap capture to clipboard'
  24. EXETYPE        WINDOWS
  25. STUB           'WINSTUB.EXE'
  26. CODE           PRELOAD MOVEABLE
  27. DATA           PRELOAD MOVEABLE MULTIPLE
  28. HEAPSIZE       1024
  29. STACKSIZE      4096
  30. EXPORTS        WndProc
  31.  
  32.  
  33.  
  34. [LISTING THREE]
  35.  
  36. /* snap3.rc */
  37. #include "snap3.h"
  38.  
  39. snap3    ICON    snap3.ico
  40. snap3 MENU
  41. BEGIN
  42.     MENUITEM "&Start Capture"    IDM_START
  43.     MENUITEM "&Clear Buffer",    IDM_CLEAR
  44.     MENUITEM "&About",            IDM_ABOUT
  45.     MENUITEM "\a&Help",            IDM_HELP
  46. END
  47.  
  48.  
  49.  
  50.  
  51. [LISTING FOUR]
  52.  
  53. /* snap3.h */
  54.  
  55. #define IDM_START   1   /* menu item id values */
  56. #define IDM_CLEAR   2
  57. #define IDM_ABOUT   3
  58. #define IDM_HELP    4
  59.  
  60. /* function prototypes */
  61. long  FAR PASCAL WndProc (HWND, unsigned, WORD, LONG) ;
  62. void  OutlineBlock (HWND hWnd, POINT beg, POINT end) ;
  63.  
  64.  
  65.  
  66.  
  67. [LISTING FIVE]
  68.  
  69. /* snap3.C -- Screen Capture to clipboard -- jim conger  */
  70.  
  71. #include <windows.h>
  72. #include <stdlib.h>
  73. #include "snap3.h"
  74.  
  75. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine,
  76.     int nCmdShow)
  77. {
  78.      static char szAppName [] = "snap3" ;
  79.      HWND        hWnd ;
  80.      MSG         msg ;
  81.      WNDCLASS    wndclass ;
  82.  
  83.      if (!hPrevInstance) 
  84.      {
  85.           wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  86.           wndclass.lpfnWndProc   = WndProc ;
  87.           wndclass.cbClsExtra    = 0 ;
  88.           wndclass.cbWndExtra    = 0 ;
  89.           wndclass.hInstance     = hInstance ;
  90.           wndclass.hIcon         = LoadIcon (NULL, szAppName) ;
  91.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  92.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  93.           wndclass.lpszMenuName  = szAppName ;
  94.           wndclass.lpszClassName = szAppName ;
  95.  
  96.           if (!RegisterClass (&wndclass))
  97.               return FALSE ;
  98.     }
  99.     hWnd = CreateWindow (szAppName, "Snap3",
  100.          WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
  101.          GetSystemMetrics (SM_CXSCREEN) / 2,
  102.          8 * GetSystemMetrics (SM_CYMENU), 
  103.          NULL, NULL, hInstance, NULL) ;
  104.     ShowWindow (hWnd, nCmdShow) ;
  105.     UpdateWindow (hWnd) ;
  106.  
  107.     while (GetMessage (&msg, NULL, 0, 0))
  108.     {
  109.         TranslateMessage (&msg) ;
  110.         DispatchMessage (&msg) ;
  111.     }
  112.     return msg.wParam ;
  113. }
  114.  
  115. long FAR PASCAL WndProc (HWND hWnd, unsigned iMessage, WORD wParam, 
  116.     LONG lParam)
  117. {
  118.      static BOOL    bCapturing = FALSE, bBlocking = FALSE, bStarted = FALSE ;
  119.      static POINT   beg, end, oldend ;
  120.      static short   xSize, ySize ;
  121.      static HANDLE  hInstance ;
  122.      HDC            hDC, hMemDC ;
  123.      BITMAP         bm ;
  124.      HBITMAP        hBitmap ;
  125.      HICON          hIcon ;
  126.      PAINTSTRUCT    ps ;
  127.      switch (iMessage)
  128.      {
  129.          case WM_CREATE:    /* get program instance when window is created */
  130.              hInstance = GetWindowWord (hWnd, GWW_HINSTANCE) ;
  131.              break ;
  132.          case WM_COMMAND:   /* one of the menu items has been clicked */
  133.             switch (wParam)
  134.             {
  135.             case IDM_START:             /* the start capture item */
  136.                 bCapturing = TRUE ;
  137.                 bBlocking = bStarted = FALSE ;
  138.                 SetCapture (hWnd) ;                     /* grab mouse */
  139.                 SetCursor (LoadCursor (NULL, IDC_CROSS)) ;
  140.                 CloseWindow (hWnd) ;                    /* minimize window */
  141.                 break ;
  142.             case IDM_CLEAR:     /* clears screen and clipboard */
  143.                 OpenClipboard (hWnd) ;
  144.                 EmptyClipboard () ;
  145.                 CloseClipboard () ;
  146.                 InvalidateRect (hWnd, NULL, TRUE) ;     /* forces paint */
  147.                 break ;
  148.             case IDM_ABOUT:     /* show about box */
  149.                MessageBox (hWnd, "Snap3 - Windows screen capture to clipboard.
  150.                                                           \nJim Conger 1990.", 
  151.                     "Snap3 About", MB_OK) ;
  152.                 break ;
  153.             case IDM_HELP:
  154.                 MessageBox (hWnd, "After you click the Start Capture menu 
  155.                                       item, move the mouse to the upper left 
  156.                                       of the area you want to copy to the 
  157.                                       clipboard. Hold down the left mouse 
  158.                                       button while you drag the mouse to the 
  159.                                       lower right of the area. Once you release
  160.                                       the mouse button, the area is sent to the
  161.                                       clipboard and shown in Snap3's window.", 
  162.                     "Snap3 Help", MB_OK) ;
  163.                 break ;
  164.             }
  165.         case WM_LBUTTONDOWN:    /* starting capturing screen */
  166.             if (bCapturing)
  167.             {
  168.                 if (bStarted)
  169.                 {
  170.                     bBlocking = TRUE ;
  171.                     oldend = beg = MAKEPOINT (lParam) ;
  172.                     OutlineBlock (hWnd, beg, oldend) ;
  173.                     SetCursor (LoadCursor (NULL, IDC_CROSS)) ;
  174.                 }
  175.                 else
  176.                     bStarted = TRUE ;
  177.             }
  178.             break ;
  179.         case WM_MOUSEMOVE:      /* show area as rectangle on screen */
  180.             if (bBlocking)
  181.             {
  182.                 end = MAKEPOINT (lParam) ;
  183.                 OutlineBlock (hWnd, beg, oldend) ;  /* erase old outline */
  184.                 OutlineBlock (hWnd, beg, end) ;     /* draw new one */
  185.                 oldend = end ;
  186.             }
  187.             break ;
  188.         case WM_LBUTTONUP:      /* capture and send to clipboard */
  189.             if (bBlocking)
  190.             {
  191.                 bBlocking = bCapturing = FALSE ;
  192.                 SetCursor (LoadCursor (NULL, IDC_ARROW)) ;
  193.                 ReleaseCapture () ;                 /* free mouse */
  194.  
  195.                 end = MAKEPOINT (lParam) ;
  196.                 OutlineBlock (hWnd, beg, oldend) ;  /* erase area outline */
  197.                 xSize = abs (beg.x - end.x) ;
  198.                 ySize = abs (beg.y - end.y) ;
  199.                 hDC = GetDC (hWnd) ; 
  200.                 hMemDC = CreateCompatibleDC (hDC) ;
  201.                 hBitmap = CreateCompatibleBitmap (hDC, xSize, ySize) ;
  202.                 
  203.                 if (hBitmap)
  204.                 {
  205.                     SelectObject (hMemDC, hBitmap) ;
  206.                     StretchBlt (hMemDC, 0, 0, xSize, ySize, 
  207.                         hDC, beg.x, beg.y, end.x - beg.x, 
  208.                         end.y - beg.y, SRCCOPY) ;
  209.                     OpenClipboard (hWnd) ;
  210.                     EmptyClipboard () ;
  211.                     SetClipboardData (CF_BITMAP, hBitmap) ; /* copy to */
  212.                     CloseClipboard () ;                     /* clipboard */
  213.                     InvalidateRect (hWnd, NULL, TRUE) ;     /* request paint*/
  214.                 }
  215.                 else
  216.                     MessageBeep (0) ;
  217.                 DeleteDC (hMemDC) ;
  218.                 ReleaseDC (hWnd, hDC) ;
  219.             }
  220.             ShowWindow (hWnd, SW_RESTORE) ;     /* un-minimize window */ 
  221.             break ;
  222.         case WM_PAINT:      /* display contents of clipboard if bitmap */
  223.             hDC = BeginPaint (hWnd, &ps) ;
  224.             if (IsIconic (hWnd))    /* if window is iconic, show icon */
  225.             {
  226.                 hIcon = LoadIcon (hInstance, "snap3") ;
  227.                 if (hIcon != NULL)
  228.                     DrawIcon (hDC, 1, 1, hIcon) ;
  229.             }
  230.             else                    /* if not, show clipboard contents */
  231.             {               
  232.                 OpenClipboard (hWnd) ;
  233.                 if (hBitmap = GetClipboardData (CF_BITMAP)) /* if bitmap */
  234.                 {
  235.                     hMemDC = CreateCompatibleDC (hDC) ;
  236.                     SelectObject (hMemDC, hBitmap) ;
  237.                     GetObject (hBitmap, sizeof (BITMAP), (LPSTR) &bm) ;
  238.                     SetStretchBltMode (hDC, COLORONCOLOR) ;
  239.                     StretchBlt (hDC, 0, 0, xSize, ySize, hMemDC, 0, 0, 
  240.                         bm.bmWidth, bm.bmHeight, SRCCOPY) ;
  241.                     DeleteDC (hMemDC) ;
  242.                 }
  243.                 CloseClipboard () ;
  244.             }
  245.             
  246.             EndPaint (hWnd, &ps) ;
  247.             break ;
  248.         case WM_DESTROY:
  249.             PostQuitMessage (0) ;
  250.             break ;
  251.         default:
  252.             return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  253.         }
  254.    return 0L ;
  255. }
  256.  
  257. /* OutlineBlock() writes a rectangle on screen given two corner points. R2_NOT
  258.    style is used, so drawing twice on the same location erases outline. */
  259. void OutlineBlock (HWND hWnd, POINT beg, POINT end)
  260. {
  261.      HDC   hDC ;
  262.  
  263.      hDC = CreateDC ("DISPLAY", NULL, NULL, NULL) ;
  264.      ClientToScreen (hWnd, &beg) ;      /* convert to screen units */
  265.      ClientToScreen (hWnd, &end) ;
  266.      SetROP2 (hDC, R2_NOT) ;            /* use logical NOT brush */
  267.      MoveTo (hDC, beg.x, beg.y) ;       /* draw rectangle */
  268.      LineTo (hDC, end.x, beg.y) ;
  269.      LineTo (hDC, end.x, end.y) ;
  270.      LineTo (hDC, beg.x, end.y) ;
  271.      LineTo (hDC, beg.x, beg.y) ;
  272.      DeleteDC (hDC) ;
  273. }
  274.  
  275.